﻿/******************************************************************************
    Copyright (C) 2020 by Zaodao(Dalian) Education Technology Co., Ltd.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#ifndef RTC_RECORDER_H_
#define RTC_RECORDER_H_

#include <QObject>
#include <QString>
#include <QByteArray>

#include "obs.hpp"
#include "obs-source.h"
#include "util/platform.h"
#include "recorder-define.h"
#include "engine_types.h"

// RTC录制配置结构体
struct RTCRecorderConfig {
    QString logPath;              // 日志文件路径
    QString crashPath;            // 崩溃日志路径
    QString userName;             // 用户名
    QString userId;               // 用户ID
    int sampleRate;               // 音频采样率
    int channels;                 // 音频通道数
    QString audioFormat;          // 音频格式
    double outputGain;            // 输出音频增益（扬声器）
    bool enableNoiseSuppress;     // 是否启用降噪（仅对扬声器音频）
    
    RTCRecorderConfig() : sampleRate(44100), channels(2), outputGain(0.0), enableNoiseSuppress(false) {}
};

class RTCRecorder : public QObject
{
    Q_OBJECT

public:
    explicit RTCRecorder(QObject *parent = nullptr);
    ~RTCRecorder();

    // 初始化RTC录制器
    bool initialize(const RTCRecorderConfig &config);
    
    // 确保OBS设置已完成（延迟初始化）
    bool ensureOBSSetup();
    
    // 处理RTC音频帧（麦克风音频）
    void processAudioFrame(const ding::rtc::RtcEngineAudioFrame &frame);
    
    // 设置配置
    void setConfig(const RTCRecorderConfig &config) { m_config = config; }
    const RTCRecorderConfig &getConfig() const { return m_config; }
    
    // 音频控制功能（仅对扬声器音频）
    void setOutputGain(float value);
    void setOutputMute(bool mute);
    void addAudioFilter(const QString &name, const QString &type);
    
    // 录制控制功能
    void startRecording();
    void stopRecording();
    bool isRecording() const { return m_isRecording; }

public slots:
    // RTC录制控制（只处理RTC特有的音频帧处理）
    void startAudioProcessing();
    void stopAudioProcessing();
    
    // OBS设置（响应OBS初始化完成信号）
    void onOBSInitialized();

signals:
    // RTC音频处理状态信号
    void audioProcessingStarted();
    void audioProcessingStopped();

private:
    // 创建RTC音频源
    bool createRTCAudioSource();
    
    // 创建RTC输出源
    bool createRTCOutputSource();
    
    // 设置音频通道
    bool setAudioChannels();
    
    // 设置OBS音频输出通道
    bool setupOBSAudioChannels();
    
    // 清理OBS音频输出通道
    void cleanupOBSAudioChannels();

private:
    RTCRecorderConfig m_config;
    obs_source_t *m_rtcAudioSource;      // RTC麦克风音频源
    obs_source_t *m_rtcOutputSource;     // 扬声器音频源
    bool m_initialized;
    bool m_needsOBSSetup;                // 标记是否需要进行OBS设置
    bool m_isRecording;                  // 标记是否正在录制
    int m_audioProcessingRefCount;       // 音频处理引用计数（支持同时本地录制和推流录制）
    
    // 统计信息
    int m_totalFrameCount;               // 总帧数
    int m_successfulFrameCount;          // 成功处理的帧数
    bool m_firstFrameLogged;             // 是否已记录第一帧
};

#endif  // RTC_RECORDER_H_ 